package com.yourname.xhs.service;

import com.yourname.xhs.bean.Category;
import com.yourname.xhs.bean.Note;
import com.yourname.xhs.dao.NoteDaoImpl;
import com.yourname.xhs.entity.User;

import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class NoteServiceImpl implements NoteService{
    Scanner sc=new Scanner(System.in);
    NoteDaoImpl ndi=new NoteDaoImpl();


    //创建笔记
    public boolean CreateNote(String title,String content,User user) throws SQLException {
        Note note = new Note();
        //查询笔记id，创建时间 算了 交给查询业务吧
        note.setTitle(title);
        note.setContent(content);
        note.setAuthor(user);
        System.out.println("-------------------");
        System.out.println("请输入要发布的区域：");
        System.out.println("1、知识区");
        System.out.println("2、美食区");
        System.out.println("3、旅游区");
        System.out.println("4、运动区");
        System.out.println("5、生活区");
        System.out.println("6、科技区");
        System.out.println("-------------------");
        String categoryName;
        int choice= sc.nextInt();
        switch (choice){
            case 1:
                note.setCategory(Category.KNOWLEDGE);
                categoryName=Category.KNOWLEDGE.name();
                break;
            case 2:
                note.setCategory(Category.FOOD);
                categoryName=Category.FOOD.name();
                break;
            case 3:
                note.setCategory(Category.TRAVEL);
                categoryName=Category.TRAVEL.name();
                break;
            case 4:
                note.setCategory(Category.SPORTS);
                categoryName=Category.SPORTS.name();
                break;
            case 5:
                note.setCategory(Category.LIFE);
                categoryName=Category.LIFE.name();
                break;
            case 6:
                note.setCategory(Category.TECHNOLOGY);
                categoryName=Category.TECHNOLOGY.name();
                break;
            default:
                return false;


        }
        return ndi.createNote(note,user,categoryName);
    }





    //查看全部笔记
    public List<Note> selectAllNotes() throws Exception {
        return ndi.selectAllNotes();
    }

    //查看个人笔记
    public List<Note> selectPersonalNote(User user) throws Exception {
        return ndi.selectPersonalNote(user);
    }


    //查看分类笔记
    public List<Note> selectTypeNote() throws Exception {

        System.out.println("-------------------");
        System.out.println("请输入要查询的分类：");
        System.out.println("1、知识区");
        System.out.println("2、美食区");
        System.out.println("3、旅游区");
        System.out.println("4、运动区");
        System.out.println("5、生活区");
        System.out.println("6、科技区");
        System.out.println("-------------------");
        String categoryName = null;

        int choice = sc.nextInt();
        sc.nextLine();
        switch (choice) {
            case 1:
                categoryName = Category.KNOWLEDGE.name();
                break;
            case 2:
                categoryName = Category.FOOD.name();
                break;
            case 3:
                categoryName = Category.TRAVEL.name();
                break;
            case 4:
                categoryName = Category.SPORTS.name();
                break;
            case 5:
                categoryName = Category.LIFE.name();
                break;
            case 6:
                categoryName = Category.TECHNOLOGY.name();
                break;
            default:
                System.out.println("请重新输入：");
                break;

        }


        return ndi.selectTypeNotes(categoryName);
    }


    //删除笔记 所有
    public int deleteNoteById(Long id) {
        return ndi.deleteNoteById(id);
    }


    //个人
    public int deletePersonalNoteById(Long id,User user) {
        return ndi.deletePersonalNoteById(id,user);
    }




    //修改笔记 标题
    public int updateTitle(Long id,User user, String newTitle){
        return ndi.updateTitle(id,user,newTitle);
    }
  //内容
    public int updateContent(Long id,User user, String newContent){
        return ndi.updateContent(id,user,newContent);
    }
   //种类
    public int updateCategory(Long id,User user, String newCategory){
        return ndi.updateCategory(id,user,newCategory);
    }



    class NoteNotFoundException extends Exception {
        public NoteNotFoundException(String message) {
            super(message);
        }
    }



    //搜索笔记
    public List<Note> searchNotesByTitle(String keyword) throws SQLException {
        return ndi.searchNotesByTitle(keyword);
    }

}




